<?php
namespace Admin\Model;
use Think\Model;
class SpecialcourseModel extends Model {

     public function _before_insert(&$data,$option){
        if($_FILES['attach']['tmp_name']){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     31457280 ;// 设置附件上传大小
            $upload->exts      =     array( 'txt', 'pdf','docx','ppt');// 设置附件上传类型
            // $upload->autoSub = false;
            $upload->savePath  =      './Public/UPLOADS/specialcourse/'; // 设置附件上传目录
            $upload->rootPath  =      './'; 
            $upload->saveName ='';
            $info   =   $upload->uploadOne($_FILES['attach']);
            if(!$info) {// 上传错误提示错误信息
                //$this->error($upload->getError());
                var_dump($upload->getError());exit;
            }
            // dump($info);die();
            $thumb=$info['savepath'].$info['name']; 
            $data['attach']=$thumb;
        }
            $data['restticket']=$data['totalticket'];
            $data['scdate']=date('y-m-d h:i:s',time());
    }

     public function _before_update(&$data,$option){
        $enddate=I('_enddate');
        $startdate=I('_startdate');
        if($_FILES['attach']['tmp_name']){
            if(I('oldpic')){
                if(file_exists(I('oldpic'))){
                @unlink(I('oldpic'));
                }
            }
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array( 'txt', 'pdf','docx','ppt');// 设置附件上传类型
            // $upload->autoSub = false;
             $upload->saveName ='';
            $upload->savePath  =      './Public/UPLOADS/specialcourse/'; // 设置附件上传目录
            $upload->rootPath  =      './'; 
            $info   =   $upload->uploadOne($_FILES['attach']);
            $thumb=$info['savepath'].$info['name'];
            $data['attach']=$thumb;
        }

        if (!$data['startdate']) {
           $data['startdate']=$startdate;
        }
        if (!$data['enddate']) {
           $data['enddate']=$enddate;
        }
    }
    public function _before_delete($option){
        $id=$option['where']['scid'];
        $res=$this->field('attach')->find($scid);
        if($res['attach']){
            //dump($res['attach']);
         if(file_exists($res['attach'])){
               unlink($res['attach']);
             }else{
                echo '不存在文件';
             }
        }
    }

}